/*******************************************************************************
 * Copyright 2009 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
/**
 * Place holder for copyright, needs to be replaced before release.
 */
package com.softao.ming.gwt.core.internal.runtime.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.xml.client.Element;
import com.softao.ming.gwt.core.runtime.client.application.IExtension;
import com.softao.ming.gwt.core.runtime.client.application.IExtensionPoint;

/**
 * 
 */
public class ExtensionPoint implements IExtensionPoint {
	private final String mUniqueIdentifier;
	private final List<IExtension> mExtensions;
	private final Map<String, IExtension> mExtensionMap;
	
	/**
	 * @param extensionElement 
	 */
	public ExtensionPoint(Element extensionElement) {
		mUniqueIdentifier = extensionElement.getAttribute("point");
		mExtensions = new ArrayList<IExtension>();
		mExtensionMap = new HashMap<String, IExtension>();
	}
	
	void addExtension(IExtension extension) {
		mExtensions.add(extension);
		mExtensionMap.put(extension.getUniqueIdentifier(), extension);
	}
	
	@Override
	public IExtension[] getExtensions() {
		return mExtensions.toArray(new IExtension[mExtensions.size()]);
	}

	@Override
	public String getUniqueIdentifier() {
		return mUniqueIdentifier;
	}

	@Override
	public IExtension getExtension(String extensionId) {
		if (mExtensionMap.containsKey(extensionId)) {
			return mExtensionMap.get(extensionId);
		} else {
			return null;
		}
	}

	@Override
	public int hashCode() {
		return mUniqueIdentifier == null ? 0 : mUniqueIdentifier.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ExtensionPoint other = (ExtensionPoint) obj;
		if (mUniqueIdentifier == null) {
			if (other.mUniqueIdentifier != null)
				return false;
		} else if (!mUniqueIdentifier.equals(other.mUniqueIdentifier))
			return false;
		return true;
	}

	@Override
	public IExtension getExtensionByRootConfigurationElementId(
			String rootElementId) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
